var app = getApp()
var appData = app.globalData
var co = require('../../js/common.js')
Page({
    data: {
        isShowInputLabelArr: [false, false, false, false],
        tableInfo: null,
        cate: null,
        pick_i: -1
    },

    onLoad: function (options) {
        var o = options
        if (!co.isEmpty(o.tab)) {
            var obj = JSON.parse(o.tab);
            if (obj.table) {
                this.setData({
                    tableInfo: obj.table,
                    pick_i: obj.cate.idArr.indexOf(obj.table.fenlei),
                    isShowInputLabelArr: [true, true, true, true],
                })
            }

            this.setData({
                cate: obj.cate,
            })
        }
    },

    pickerClick: function (e) {
        console.log(e);
        console.log('pickerClick:e');
        this.setData({//页面用分类数据
            pick_i: e.detail.value,
            isShowInputLabelArr: [true, false, false, false],
        })
    },

    submitTableInfo: function (e) {
        var param = e.detail.value
        param.cate = this.data.cate.idArr[this.data.pick_i]
        param.uid = appData.shopInfo.userid
        var table = this.data.tableInfo
        if (table) {
            param.id = table.id
            param.sql = 'update'
        } else {
            param.sql = 'insert'
        }
        console.log(param)
        var that = this
        app.getJsonData('api/admin/table_save.php', param, function (res) {
            console.log(res)
            console.log('res')
            var str = JSON.stringify(res)
            that.setData({
                test: str
            })
            that.resultHandle(res)
        })
    },

    resultHandle: function (res) {
        if (res.code == 0) {
            res.data.pick_i = this.data.pick_i
            app.noticeUpdateTableListAdmin(res.data)
            wx.navigateBack()
        }else{
            wx.showToast({
                title: res.message,
            })
        }
    },

    showInputLabel: function (e) {
        if (e.detail.value.length > 1) return;
        var index = Number(e.currentTarget.id);
        var isShowArray = this.data.isShowInputLabelArr;
        isShowArray[index] = true;

        this.setData({
            isShowInputLabelArr: isShowArray
        })
    },

    onShareAppMessage: function () {

    }
})